(function() {
  app.directive('alertTip', function() {
    return {
      restrict: 'E',
      template: '<div ng-if="ctrl.message">' +
        '<div class="alert-tip">' +
        '<span ng-class="{\'alert_success_state\':$parent.ctrl.state===1,\'alert_fail_state\':$parent.ctrl.state===0}"></span>' +
        '<span ng-bind="$parent.ctrl.message"></span>' +
        '</div>' +
        '<div class="alert-tip-model" id="alert-tip-model"></div>' +
        '</div>',
      replace: true,
      scope: {
        message: '=',
        state: '='
      },
      controller: function($scope, alertTip) {
        $scope.ctrl = alertTip;
      }
    };
  });

  app.factory('alertTip', function($timeout) {
    return {
      message: null,
      state: 0,
      open: function(obj) {
        this.message = obj.msg || '';
        this.state = obj.state || 0;
        var _self = this;
        $timeout(function() {
          _self.clear();
        }, 2500);
      },
      clear: function() {
        this.message = null;
        this.state = 0;
      },
      alert: function(name) { // 调用修改后的系统alert
        var iframe = document.createElement('IFRAME');
        iframe.style.display = 'none';
        iframe.setAttribute('src', 'data:text/plain,');
        document.documentElement.appendChild(iframe);
        window.frames[0].window.alert(name);
        iframe.parentNode.removeChild(iframe);
      }
    };
  });

})();
